package com.lan.iot.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lan.iot.R;
import com.lan.iot.RAdapterz;

public class VPFz extends VPFragment {
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "img";
    public static VPFz newInstance(String title, int img) {
        VPFz fragment = new VPFz();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, title);
        args.putInt(ARG_PARAM2,img);
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_v_p, container, false);
        Bundle argument = getArguments();
        final RAdapterz mAdapter;
        RecyclerView mRecyclerView;
        mRecyclerView=view.findViewById(R.id.recycler_view);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL,false);

        mRecyclerView.setLayoutManager(gridLayoutManager);
        mAdapter = new RAdapterz(getContext());
        mRecyclerView.setAdapter(mAdapter);

        /**
         * 设置网格布局每个item的间距
         */
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //item1距离底部的距离
                outRect.bottom = 30;
                //item距离左侧的距离
                outRect.left = 30;
            }
        });
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.CART_BROADCAST_a_2");
        BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean   msg_a=intent.getBooleanExtra("acdndata",false);
                mAdapter.refreshAcdn(msg_a);
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
        IntentFilter intentFilterz = new IntentFilter();
        intentFilterz.addAction("android.intent.action.CART_BROADCAST_a_2z");
        BroadcastReceiver mItemViewListClickReceiverz = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean   msg_a=intent.getBooleanExtra("zdata",false);
                mAdapter.refreshz(msg_a);
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiverz, intentFilterz);
        IntentFilter intentFilterc = new IntentFilter();
        intentFilterc.addAction("android.intent.action.CART_BROADCAST_a_2c");
        BroadcastReceiver mItemViewListClickReceiverc = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                Boolean   msg_a=intent.getBooleanExtra("cdata",false);
                mAdapter.refreshc(msg_a);
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiverc, intentFilterc);
//        ImageView iv = view.findViewById(R.id.iv);
//        iv.setImageResource(argument.getInt(ARG_PARAM2,R.drawable.ji1));
        return view;
    }
}
